package Binrary_Management_System.ioPeration;

import Binrary_Management_System.Book.Book;
import Binrary_Management_System.Book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 何华树
 * Date: 2024-09-13
 * Time: 14:04
 */
public class AddOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("新增图书！");

        int currentSize = bookList.getUseSize();

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要新增的书的书名：");
        String name = scanner.nextLine();
        System.out.println("请输入你要新增的书的作者：");
        String author = scanner.nextLine();
        System.out.println("请输入你要新增的书的类型：");
        String type = scanner.nextLine();
        System.out.println("请输入你要新增的书的价格：");
        int price = scanner.nextInt();

        Book book = new Book(name,author,type,price);
        int i = 0;
        for (; i < currentSize; i++) {
            if((bookList.getBook(i).getName().equals(name) &&
                bookList.getBook(i).getAuthor().equals(author) &&
                bookList.getBook(i).getType().equals(type))) {
                System.out.println("你要新增的图书已经存在，无法新增相同的图书！");
                return;
            }
        }
        if(i == currentSize){
            bookList.setBook(currentSize,book);
            bookList.setUseSize(currentSize+1);
            System.out.println("添加成功！");
        }
    }
}
